package controller;

import java.io.*;
import java.sql.*;
import java.util.ResourceBundle;

public class DatabaseConnection {
	private static DatabaseConnection INSTANCE = new DatabaseConnection();
	private Connection conn;

	private DatabaseConnection() {
	}

	public static DatabaseConnection getInstance() {
		return INSTANCE;
	}

	public Connection getConnection() throws Exception, Exception {
		if (conn == null || (conn != null && conn.isClosed())) {
			conn = makeConnection();
		}
		return conn;
	}

	private Connection makeConnection() throws ClassNotFoundException,
			SQLException {
		Class.forName("org.gjt.mm.mysql.Driver");
		ResourceBundle bundle = ResourceBundle.getBundle("database");
		return conn = DriverManager.getConnection(bundle.getString("db.url")
				+ "/" + bundle.getString("db.schema"),
				bundle.getString("db.user"), bundle.getString("db.password"));

	}

	@Override
	protected void finalize() throws Throwable {
		if (conn != null) {
			conn.close();
		}
		super.finalize();
	}
}
